package cn.edu.zafu.bigdata.lyq.work10;
import java.util.Scanner;

public class StudentManagementSystem {
    public static void main(String[] args) {
        DBUtil.createTable();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine();  // 处理输入缓冲区的换行符
            switch (choice) {
                case 1:
                    System.out.print("请输入学号: ");
                    String id = scanner.nextLine();
                    System.out.print("请输入姓名: ");
                    String name = scanner.nextLine();
                    System.out.print("请输入电话: ");
                    String phone = scanner.nextLine();
                    Student student = new Student(id, name, phone);
                    DBUtil.addStudent(student);
                    break;
                case 2:
                    System.out.print("请输入要检索的学号（输入QUIT退出检索）: ");
                    String searchId = scanner.nextLine();
                    while (!searchId.equals("QUIT")) {
                        Student foundStudent = DBUtil.getStudentById(searchId);
                        if (foundStudent!= null) {
                            System.out.println(foundStudent);
                        } else {
                            System.out.println("未找到该学生");
                        }
                        System.out.print("请输入要检索的学号（输入QUIT退出检索）: ");
                        searchId = scanner.nextLine();
                    }
                    break;
                case 3:
                    System.out.print("请输入要更新信息的学生学号（输入QUIT退出更新）: ");
                    String updateId = scanner.nextLine();
                    while (!updateId.equals("QUIT")) {
                        Student existingStudent = DBUtil.getStudentById(updateId);
                        if (existingStudent!= null) {
                            System.out.print("请输入新的姓名: ");
                            String newName = scanner.nextLine();
                            System.out.print("请输入新的电话: ");
                            String newPhone = scanner.nextLine();
                            existingStudent.setName(newName);
                            existingStudent.setPhone(newPhone);
                            DBUtil.updateStudent(existingStudent);
                        } else {
                            System.out.println("未找到该学生");
                        }
                        System.out.print("请输入要更新信息的学生学号（输入QUIT退出更新）: ");
                        updateId = scanner.nextLine();
                    }
                    break;
                case 4:
                    System.out.print("请输入要删除的学生学号（输入QUIT退出删除）: ");
                    String deleteId = scanner.nextLine();
                    while (!deleteId.equals("QUIT")) {
                        DBUtil.deleteStudent(deleteId);
                        System.out.print("请输入要删除的学生学号（输入QUIT退出删除）: ");
                        deleteId = scanner.nextLine();
                    }
                    break;
                case 5:
                    System.out.println("程序已退出，保存所有修改。");
                    System.exit(0);
                default:
                    System.out.println("输入的操作编号无效，请重新输入！");
            }
        }
    }
}